#!/bin/sh

SYSTEMD_EXE="/lib/systemd/systemd --unit=basic.target"
SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')"
if [ "$LOGNAME" != "root" ] && ( [ -z "$SYSTEMD_PID" ] || [ "$SYSTEMD_PID" != "1" ] ); then
    export | sed -e 's/^declare -x //;/^IFS=".*[^"]$/{N;s/\n//}' | \
        grep -E -v "^(BASH|BASH_ENV|DIRSTACK|EUID|GROUPS|HOME|HOSTNAME|\
IFS|LANG|LOGNAME|MACHTYPE|MAIL|NAME|OLDPWD|OPTERR|\
OSTYPE|PATH|PIPESTATUS|POSIXLY_CORRECT|PPID|PS1|PS4|\
SHELL|SHELLOPTS|SHLVL|SYSTEMD_PID|UID|USER|_)(=|\$)" > "$HOME/.systemd-env"
    export SYSTEMD_PRE_NAMESPACE_PATH="$PATH"
    export SYSTEMD_PRE_NAMESPACE_PWD="$PWD"
    exec sudo /usr/sbin/enter-systemd-namespace "$BASH_EXECUTION_STRING"
fi

if [ -n "$SYSTEMD_PRE_NAMESPACE_PATH" ]; then
    export PATH="$SYSTEMD_PRE_NAMESPACE_PATH"
    unset SYSTEMD_PRE_NAMESPACE_PATH
fi
if [ -n "$SYSTEMD_PRE_NAMESPACE_PWD" ]; then
    cd "$SYSTEMD_PRE_NAMESPACE_PWD"
    unset SYSTEMD_PRE_NAMESPACE_PWD
fi
